package com.dqyt.app.service;

import com.alibaba.fastjson.JSON;
import com.dqyt.app.bean.LocaGzlqrd;
import com.dqyt.app.mongo.LocaGzlqrdDao;
import com.dqyt.common.utils.UserInfoUtil;
import com.dqyt.khjc.bean.ResultMsg;
import com.dqyt.khjc.bean.User;
import com.dqyt.khjc.utils.TimeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 工作量确认单 本地
 */
@Service
public class AppLocaGzlqrdService {


    @Autowired
    private LocaGzlqrdDao locaGzlqrdDao;


    /**
     * 分页查询待批准工作量确认单 本地
     * @param request
     * @param page
     * @param size
     * @return
     */
    public ResultMsg getQrdListPage2(HttpServletRequest request, String token,long userId, Integer page, Integer size) {

        ResultMsg ret = new ResultMsg(); // 声明一个返回值
        try {
            ret = UserInfoUtil.isAuthori_App(userId, token); // 验证用户是否登录
            if (null != ret) {
                return ret;
            }
            if (page != null && size != null) {
                page = (page - 1) * size;
            }
            ret = new ResultMsg();
            List<LocaGzlqrd>  data =  locaGzlqrdDao.getLocaGzlqrdDpzPage(page,size,"",0,0);
            ret.setCode(ResultMsg.SUCESS);
            ret.setData(JSON.toJSONString(data));
            return ret;
        }catch (Exception e) {
            ret.setCode(ResultMsg.ERROR);
            ret.setMsg("未知的错误，请稍候再试！");
            e.printStackTrace();
        }
        return ret;
    }


    /**
     * 设置 驳回工作量确认单
     * @param request
     * @return
     */
    public ResultMsg rejectQrdDal2(HttpServletRequest request,String token, long userId,String rwdbh,String bhgyy){
        ResultMsg ret = new ResultMsg(); // 声明一个返回值
        try {
            // 检查用户是否登录Token是否正确
            ret = UserInfoUtil.isAuthori_App(userId, token); // 验证用户是否登录
            // 如果有错误信息
            if (ret != null) {
                return ret;
            }
            ret = new ResultMsg(); // 声明一个返回值
            LocaGzlqrd locaGzlqrd =  locaGzlqrdDao.getLocaGzlqrdById(rwdbh);
            locaGzlqrd.bhgyy = bhgyy;
            locaGzlqrd.start = 0;
            locaGzlqrd.jydwFzrId =0;
            locaGzlqrd.jydwFzr ="";
            locaGzlqrdDao.save(locaGzlqrd);
            ret.setCode(ResultMsg.SUCESS);
            ret.setMsg("操作成功");
            return ret;
        }catch (Exception e) {
            ret.setCode(ResultMsg.ERROR);
            ret.setMsg("未知的错误，请稍候再试！");
            e.printStackTrace();
        }
        return ret;
    }


    /**
     * 设置 通工作量确认单
     * @param request
     * @return
     */
    public ResultMsg srtQrdOkProgress2(HttpServletRequest request,String token, long userId,String rwdbh){
        ResultMsg ret = new ResultMsg(); // 声明一个返回值
        try {
            // 检查用户是否登录Token是否正确
            ret = UserInfoUtil.isAuthori_App(userId, token); // 验证用户是否登录
            // 如果有错误信息
            if (ret != null) {
                return ret;
            }
            User muser = UserInfoUtil.getUserInfologin(userId,token);
            ret = new ResultMsg(); // 声明一个返回值
            if(null == muser){
                ret.setCode(ResultMsg.ERROR);
                ret.setMsg("登录过期");
                return ret;
            }

            String qzrq = TimeUtil.formatTime_tow(TimeUtil.currentTimeMillis());
            LocaGzlqrd locaGzlqrd =  locaGzlqrdDao.getLocaGzlqrdById(rwdbh);
            locaGzlqrd.start =2;
            locaGzlqrd.jydwFzrId =muser.getUserId();
            locaGzlqrd.jydwFzr = muser.getUserName();
            locaGzlqrd.qzrq =qzrq;
            locaGzlqrdDao.save(locaGzlqrd);
            ret.setCode(ResultMsg.SUCESS);
            ret.setMsg("操作成功");


        }catch (Exception e) {
            ret.setCode(ResultMsg.ERROR);
            ret.setMsg("未知的错误，请稍候再试！");
            e.printStackTrace();
        }
        return ret;
    }



}
